package IBBB.view;

import IBBB.model.UserMessage;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Side;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import IBBB.network.sendmessage.send_changeplan;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class PersonalScheduleBox {
    @FXML
    private ImageView wordStockImage;
    @FXML
    private Label leftDays;
    @FXML
    private Text messageText;
    @FXML
    private ChoiceBox selectPlanBox;
    @FXML
    private PieChart scheduleChart;
    @FXML
    private Button confirmButton;
    @FXML
    private Button deleteButton;
    private StringBinding wordDesc;
    private UserMessage userMessage;
    private String wordStockName;
    private NumberBinding days;
    private NumberBinding part;
    private ObservableList<PieChart.Data> data;
    private Integer currentPlan;

    private void warn(String s, String t) {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("WarningPane.fxml"));
        try {
            Parent parent = loader.load();
            WarningPane wp = (WarningPane)loader.getController();
            wp.setMessage(s);
            Stage w = new Stage(StageStyle.UTILITY);
            w.setTitle(t);
            w.setScene(new Scene(parent, 350,220));
            w.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void initialize() {
        selectPlanBox.setItems(FXCollections.observableArrayList(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60));
    }

    public void init(UserMessage userMessage, StringProperty name, IntegerProperty sche, Integer totalNum, IntegerProperty plan) {
        this.userMessage = userMessage;
        wordStockName = name.get();
        currentPlan = plan.get();
        wordDesc = Bindings.createStringBinding(()-> name.get() + "  总词数：" + String.valueOf(totalNum) + "  进度：" + String.valueOf(sche.get()), name, sche);
        messageText.setText(wordDesc.getValue());
        messageText.textProperty().bind(wordDesc);
        selectPlanBox.setValue(plan.get());
        //selectPlanBox.setDisable(true);
        days = Bindings.divide(sche.multiply(-1).add(totalNum), plan);
        leftDays.setText(((Integer)days.getValue()).toString());
        days.addListener(((observableValue, number, t1) -> leftDays.setText(((Integer)t1).toString())));

        part = sche.divide((double)totalNum);
        data = FXCollections.observableArrayList(
                new PieChart.Data("已学习", (double)part.getValue()),
                new PieChart.Data("未学习", 1 - (double)part.getValue())
        );
        data.get(0).pieValueProperty().bind(part);
        data.get(1).pieValueProperty().bind(part.multiply(-1).add(1));
        scheduleChart.setData(data);
        scheduleChart.setLegendSide(Side.LEFT);
        scheduleChart.setTitle("学习进度");
        wordStockImage.setImage(new Image(name.get() + ".png"));
    }

    @FXML
    private void setConfirmButtonAction() {
        confirmButton.setDisable(true);
        Integer selected = (Integer) selectPlanBox.getValue();
        if (!selected.equals(currentPlan)) {
            send_changeplan change = new send_changeplan("localhost", userMessage.getUserName().get(), wordStockName, selected);
            Thread t = new Thread(change);
            change.messageProperty().addListener(((observableValue, s, t1) -> {
                switch (t1) {
                    case "timeout": {
                        warn("网络异常", "警告");
                        confirmButton.setDisable(false);
                        break;
                    }

                    case "wrong": {
                        warn("出现异常，更新失败", "警告");
                        confirmButton.setDisable(false);
                        break;
                    }

                    case "success": {
                        userMessage.changeSchedule(wordStockName, selected);
                        currentPlan = selected;
                        confirmButton.setDisable(false);
                    }
                }
            }));
            t.start();
        }
    }

    @FXML
    private void setDeleteButtonAction() {
        deleteButton.setDisable(true);
        send_changeplan change = new send_changeplan("localhost", userMessage.getUserName().get(), wordStockName, 0);
        Thread t = new Thread(change);
        change.messageProperty().addListener(((observableValue, s, t1) -> {
            switch (t1) {
                case "timeout": {
                    warn("网络异常", "警告");
                    deleteButton.setDisable(false);
                    break;
                }

                case "wrong": {
                    warn("出现异常，更新失败", "警告");
                    deleteButton.setDisable(false);
                    break;
                }

                case "success": {
                    userMessage.deleteSchedule(wordStockName);
                    deleteButton.setDisable(false);
                    break;
                }
            }
        }));
        t.start();
    }
}
